from sqlalchemy.orm import Session
from app.database import engine
from app.schemas.user_condition import Condition
from app.models.user import UserCondition as ConditionModel


class UserCondition(object):

    @staticmethod
    def create(user_id: int, form: Condition):
        with Session(engine) as session:
            form.uid = user_id
            obj = session.query(ConditionModel).filter_by(uid=user_id)
            condition = ConditionModel(**form.dict())
            if obj.first() is None:
                session.add(condition)
            else:
                obj.update(form.dict())
            session.commit()
            return True
